local addonName = "PSLibBar";
local Addon = LibStub("AceAddon-3.0"):NewAddon(addonName, "AceEvent-3.0");
------------------------------------------------------------
DEBUG_ENABLE(true);
------------------------------------------------------------
local function UpdateModuleDB(addonDB,module,reset)
	if(not addonDB[module.name]) then reset = true; end
	if(not module.name) then return; end

	if(addonDB[module.name] and not reset) then 
		module.db = addonDB[module.name]; 
	elseif(not module.db or reset) then
		local defaults = PS:CloneObject(module.defaults) or nil;
		module.db = defaults;
		addonDB[module.name] = defaults;
	end
	
	if(module["OnSettingsChanged"] and module.db) then 
		module["OnSettingsChanged"](module, module.db); 
		LibStub("AceConfigRegistry-3.0"):NotifyChange(addonName);
	end
end

Addon:SetDefaultModuleState(true);
Addon:SetDefaultModulePrototype(
{
	GetValue = function(self, info)
		local path = self.db;
		for i=2,#info-1 do
			if(path[info[i]]) then
				path = path[info[i]] or {};
			end
		end
		return path[info[#info]];
	end,

	SetValue = function(self, info, value)
		local path = self.db;
		for i=2,#info-1 do
			if(path[info[i]]) then
				path = path[info[i]] or {};
			end
		end

		path[info[#info]] = value;
		
		if(self["OnSettingsChanged"]) then 
			self["OnSettingsChanged"](self, { [info[#info]] = value }); 
		end
		LibStub("AceConfigRegistry-3.0"):NotifyChange(addonName);
	end,
});
------------------------------------------------------------

------------------------------------------------------------
-- AceAddon implementation ---------------------------------
------------------------------------------------------------
function Addon:OnInitialize()
	self.db = LibStub("AceDB-3.0"):New("DB_PSLib_Bar", { profile = { } }, true);		
	self.db.RegisterCallback(self, "OnProfileChanged", "ProfileChanged")
	self.db.RegisterCallback(self, "OnProfileCopied",  "ProfileChanged")
	self.db.RegisterCallback(self, "OnProfileReset",   "ProfileReset")

	self:UpdateSettings();
	
	local AceGUI = LibStub("AceGUI-3.0");
	local window = AceGUI:Create("Window");
	window:SetTitle("Title");
	window:SetLayout("PSLib.Grid");
	
	local f = AceGUI:Create("PSLib.SimpleGroup");
	f:SetLayout("PSLib.Grid")
	f.content.layoutSettings = 
	{
		columns = { "1*" },
		rows = { "Auto", 160, "1*", "1*", "1*"}, 
		--cellSpacing = 1,
	}
	
	local checkBox1 = AceGUI:Create("PSLib.CheckBox");
	checkBox1:SetText("A unique identifier for the achievements; usable with various Achievement API functions.");
	checkBox1:SetLayoutSettings( { row = 1 } );
	
	f:AddChild(checkBox1);
	local checkBox2 = AceGUI:Create("PSLib.CheckBox");
	checkBox2:SetLayoutSettings( { row = 2 } );
	checkBox2:SetCallback("OnValueChanged", function(self, event, value)
		if(value) then
			checkBox1:Enable();
		else
			checkBox1:Disable();
		end
	end);
	checkBox2:SetText("A unique identifier for the achievements; usable with various Achievement API functions.");
	f:AddChild(checkBox2);
	
	local btnFrame = AceGUI:Create("PSLib.Button");
	btnFrame:SetLayoutSettings( 
		{ 
			row = 3,
			alignment = "left",
			alignmentV = "bottom",
			size = {width = 150, height = 28},
			margin = {bottom = -2}
		}
	);
	btnFrame:SetContent("Отмена");
	btnFrame:SetCornerMode("BL","SQUARE");
	btnFrame:SetCornerMode("BR","SQUARE");
	--btnFrame:SetInset(4);
	f:AddChild(btnFrame);
	
	local btnFrame2 = AceGUI:Create("PSLib.Button");
	btnFrame2:SetLayoutSettings( 
		{ 
			row = 4,
			alignment = "left",
			alignmentV = "top",
			size = {width = 150, height = 28 }
		}
	);
	btnFrame2:SetCornerMode("TL","SQUARE");
	btnFrame2:SetCornerMode("TR","SQUARE");
	--btnFrame2:SetInset(8);
	btnFrame2:SetContent("Отмена");
	btnFrame2:Disable();
	
	f:AddChild(btnFrame2);
	--f:SetVerticalScroll(20);
	window:AddChild(f);
	window:Show();
end

function Addon:OnEnable()
end

function Addon:OnDisable()
end

------------------------------------------------------------
-- AceDB Callbacks implementation --------------------------
------------------------------------------------------------
function Addon:ProfileChanged()	
	self:UpdateSettings();
end

function Addon:ProfileReset()
	self:UpdateSettings(true);
end
------------------------------------------------------------
-- Private methods implementation --------------------------
------------------------------------------------------------
function Addon:ShowConfigDialog()
	LibStub("AceConfigDialog-3.0"):Open(addonName);
end

function Addon:UpdateSettings(reset)
	for key,module in Addon:IterateModules() do
		UpdateModuleDB(self.db.profile,module,reset);
	end
	
	for key,module in Addon:IterateModules() do
		if(module["OnSettingsUpdated"]) then module["OnSettingsUpdated"](module); end
	end
end